<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use  App\Model\Price;

class PriceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
       $price = DB::table('shop_price')
        ->join('shop_goods','shop_price.gid','=','shop_goods.id')
        ->select(['shop_price.id','shop_goods.gname','shop_price.price','shop_price.store','shop_price.sales','shop_price.memory','shop_price.color'])->paginate(3);

        return view('Admin/PriceAdmin/price',['price'=>$price]);
        //return view('Admin/TypeAdmin/type',['type'=>$type]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($id)
    {
        //
        //dd($gname);
        //$goods = DB::table('shop_goods')->select(['id','gname','sales','clicknum','store','typeid','company','status','pic'])->first();
        $goods= DB::table('shop_goods')->select(['id','gname'])->where('id',$id)->get();
        //$gname = $goods->gname;
        //dd($goods->id);
        //dd($goods[0]['gname']);
        return view('Admin/PriceAdmin/create',['goods'=>$goods]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        //dd($request);
        $gid = $request->gid;
        //dd($gname);
        //$gname = $request->gname;
        $price = $request->price;
        //dd($price);
         if ($price==null) {
                    return back()->with('errorTip','价格不能为空');
                    
            }

            $patten = '/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/';
                $match = preg_match($patten,$price);
                if(!$match){
                    return back()->with('errorTip','价格不规范');
                }

        $store = $request->store;
        if ($store==null) {
                    return back()->with('errorTip','库存量不能为空');
            }
            $patten = '/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/';
                $match = preg_match($patten,$store);
                if(!$match){
                    return back()->with('errorTip','库存量不规范');
                }

        $sales= 0;

        $memory = $request->memory;
        if ($memory==null) {
                    return back()->with('errorTip','内存不能为空');
            }
            $patten = '/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/';
                $match = preg_match($patten,$memory);
                if($match){
                    return back()->with('errorTip','内存不规范');
                }
        $color = $request->color;
        if ($color==null) {
                    return back()->with('errorTip','颜色不能为空');
                    
            }
            
        $shop_price = Price::insertGetId([
                    'gid' => $gid,
                    'price' => $price,
                    'store' => $store,
                    'sales'=>$sales,
                    'memory' => $memory,
                    'color'=>$color,
                    
                ]);
         
            if($shop_price==true){

            return redirect('/admin/Price/index')->with('msg','添加成功');
             } else {
                 return back()->with('errorTip','添加失败');

            }
               



    }

    

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $price = DB::table('shop_price')->select(['id','gid','price','store','sales','memory','color'])->where('id',$id)->first();
        $gid = $price->gid;
        //dd($gid);
        $gname = DB::table('shop_goods')->select(['gname'])->where('id',$gid)->first();
        //dd($gname);
        //dd($price);
        //dd($price->id);
        return view('/Admin/PriceAdmin/edit',['price'=>$price,'gname'=>$gname]);
        //return view('Admin/PriceAdmin/create',['goods'=>$goods]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        //dd($id);
        $price = $request->price;
         if (!$price) {
                    return back()->with('errorTip','价格不能为空');
                    
            }

        $store = $request->store;
        if ($store==null) {
                    return back()->with('errorTip','库存量不能为空');
                    
            }
            
        $sales = $request->sales;
        //dd($sales);
        if ($sales==null) {
                    return back()->with('errorTip','销量不能为空');
                    
            }
            
        $memory = $request->memory;
        if (!$memory) {
                    return back()->with('errorTip','内存不能为空');
                    
            }
            
        $color = $request->color;
        if ($color==null) {
                    return back()->with('errorTip','颜色不能为空');
                    
            }
            
        //dd($price);
        $bool = DB::table('shop_price')
                ->where('id',$id)
                ->update([
                        'price'=>$price,
                        'store'=>$store,
                        'sales'=>$sales,
                        'memory'=>$memory,
                        'color'=>$color
                    
                        ]);
        //dd($bool); 
         if($bool!==0) {
                        
                  return redirect('/admin/Price/index')->with('msg','修改成功');
                     
                }else{
                 return back()->with('errorTip','修改失败，您没做任何修改');
                }
     
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        //dd($id);
         //destroy方法是执行删除功能的
        $bool = DB::table('shop_price')->where('id',$id)->delete();
        //dd($bool);

                //return back()->with('msg','删除成功');
        if($bool==1){
                 return redirect('/admin/Price/index')->with('msg','删除成功');
         }else{
                 return back()->with('msg','删除失败');
         }

    }
}
